<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>发送邮件 (web.py)</title> 
  <link rel="stylesheet" type="text/css" href="/static/webpy-new.css"/> 
  <link rel="SHORTCUT ICON" href="/static/favicon.ico"/> 
</head><body> 
 
        <div id="header"><table class="header"><tr>
                <td class="logo"><a href="/" title="Home"><img src="/static/webpy.gif" alt="" /></a></td> 
<td class="blurb">&ldquo;Think about the ideal way to write a web app.<br />Write the code to make it happen.&rdquo; [<a href="/philosophy">more...</a>]</td> </tr>
</table>
        </div> 
        <div id="location"> 
                <a href="/">home</a> &gt; 发送邮件
        </div> 
<div id="container"> 
<div id="sidebar">
<strong>get started:</strong>
<ul class="body">
  <li><a href="/install">install</a></li>
  <li><a href="/docs/0.3/tutorial">tutorial</a></li>
</ul>
<strong>learn more:</strong>
<ul class="body">
<li><a href="/docs/0.3/api">api reference</a></li>
<li><a href="/cookbook/">cookbook</a></li>
<li><a href="/src/">code examples</a></li>
</ul>

<strong>dive in:</strong>
<ul class="body"> 
<li><a href="https://github.com/webpy/webpy">source code</a></li>
<li><a href="http://groups.google.com/group/webpy/">mailing list</a></li>
<li><a href="https://github.com/webpy/webpy/issues">issue tracker</a></li>
</ul>
</div>

<div id="main"> 
<div id="content" class="body"> 
<h1>发送邮件</h1>

<h3>问题</h3>

<p>在web.py中，如何发送邮件？</p>

<h3>解法</h3>

<p>在web.py中使用<code>web.sendmail()</code>发送邮件.</p>

<pre><code>web.sendmail('cookbook@webpy.org', 'user@example.com', 'subject', 'message')
</code></pre>

<p>如果在<code>web.config</code>中指定了邮件服务器，就会使用该服务器发送邮件，否则，就根据<code>/usr/lib/sendmail</code>中的设置发送邮件。</p>

<pre><code>web.config.smtp_server = 'mail.mydomain.com'
</code></pre>

<p>如果要发送邮件给多个收件人，就给to_address赋值一个邮箱列表。</p>

<pre><code>web.sendmail('cookbook@webpy.org', ['user1@example.com', 'user2@example.com'], 'subject', 'message')
</code></pre>

<p><code>cc</code>和<code>bcc</code>关键字参数是可选的，分别表示抄送和暗送接收人。这两个参数也可以是列表，表示抄送/暗送多人。</p>

<pre><code>web.sendmail('cookbook@webpy.org', 'user@example.com', 'subject', 'message', cc='user1@example.com', bcc='user2@example.com')
</code></pre>

<p><code>headers</code>参数是一个元组，表示附加标头信息(Addition headers)</p>

<pre><code>web.sendmail('cookbook@webpy.org', 'user@example.com', 'subject', 'message',
        cc='user1@example.com', bcc='user2@example.com',
        headers=({'User-Agent': 'webpy.sendmail', 'X-Mailer': 'webpy.sendmail',})
        )
</code></pre>

<div style="clear: both;"></div> 
 
</div> 
 
</div> 
</div>
        <div id="footer"> 
    <p>started by <a style="color: gray;" href="http://www.aaronsw.com/">Aaron Swartz</a> (<a style="color: black;" href="mailto:webpy@aaronsw.com">webpy@aaronsw.com</a>)</p>

<p><a href="https://github.com/webpy/webpy.github.com/blob/master/cookbook/sendmail.zh-cn.md">fork this page on GitHub</a></p>
</div> 
 
</body> 
</html> 
